//调用这个方法就行了
//  今天昨天明天时间处理 v==3为昨天。v==6为今天。v==9为明天

export const dealTime = (v) => {
  const b = 24 * 60 * 60 * 1000 //一天的时间
  const day = new Date() //当天的时间

  v == 3
    ? day.setTime(day.getTime() - b)
    : v == 6
      ? day.setTime(day.getTime())
      : day.setTime(day.getTime() + b)

  const dayMon = day.getMonth() + 1 >= 10 ? day.getMonth() + 1 : '0' + (day.getMonth() + 1)
  const dayDat = day.getDate() + 1 >= 10 ? day.getDate() : '0' + day.getDate()

  const s = day.getFullYear() + '-' + dayMon + '-' + dayDat
  return s
}

// 时间差计算
export const difference = function (beginTime, endTime) {
  const dateBegin = new Date(beginTime)
  const dateEnd = new Date(endTime)
  const dateDiff = dateEnd.getTime() - dateBegin.getTime() //时间差的毫秒数
  const dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)) //计算出相差天数
  const leave1 = dateDiff % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
  const hours = Math.floor(leave1 / (3600 * 1000)) //计算出小时数
  //计算相差分钟数
  const leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
  const minutes = Math.floor(leave2 / (60 * 1000)) //计算相差分钟数
  //计算相差秒数
  const leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
  const seconds = Math.round(leave3 / 1000)
  console.log('相差' + dayDiff + '天' + hours + '小时' + minutes + '分钟' + seconds + '秒')
  return dayDiff
}

// 时间差计算
export const getUtc2Date = function (timestamp) {
  if (timestamp === 0) {
    return ''
  }
  const date = new Date(timestamp * 1000)
  return getNowTime_yyyyMMddHHmmss(date)
}

// 获取yyyy-MM-dd
export const getNowTime_yyyyMMdd = function (date: Date) {
  const year = date.getFullYear()
  const month = (date.getMonth() + 1).toString().padStart(2, '0')
  const day = date.getDate().toString().padStart(2, '0')
  return `${year}-${month}-${day}`
}

// 获取HHmmss
export const getNowTime_HHmmss = function (date: Date) {
  const HH = date.getHours().toString().padStart(2, '0')
  const mm = date.getMinutes().toString().padStart(2, '0')
  const ss = date.getSeconds().toString().padStart(2, '0')
  return `${HH}:${mm}:${ss}`
}

// 获取当前时间的HHmmss（增加H小时）
export const getNowTime_HHmmssSysAddHours = function (h: number) {
  const date = new Date()
  let hours = date.getHours() + h
  if (hours < 0) hours = 0
  if (hours > 23) hours = 23
  date.setHours(hours)
  const HH = date.getHours().toString().padStart(2, '0')
  const mm = date.getMinutes().toString().padStart(2, '0')
  const ss = date.getSeconds().toString().padStart(2, '0')
  return `${HH}:${mm}:${ss}`
}

// 获取当前的HHmmss
export const getNowTime_HHmmssSys = function () {
  const date = new Date()
  const HH = date.getHours().toString().padStart(2, '0')
  const mm = date.getMinutes().toString().padStart(2, '0')
  const ss = date.getSeconds().toString().padStart(2, '0')
  return `${HH}:${mm}:${ss}`
}

// 获取当前时间的HHmm（增加H小时）
export const getNowTime_HHmmSysAddHours = function (h: number) {
  const date = new Date()
  let hours = date.getHours() + h
  if (hours < 0) hours = 0
  if (hours > 23) hours = 23
  date.setHours(hours)
  const HH = date.getHours().toString().padStart(2, '0')
  const mm = date.getMinutes().toString().padStart(2, '0')
  return `${HH}:${mm}`
}

// 获取当前的HHmm
export const getNowTime_HHmmSys = function () {
  const date = new Date()
  const HH = date.getHours().toString().padStart(2, '0')
  const mm = date.getMinutes().toString().padStart(2, '0')
  return `${HH}:${mm}`
}

// 获取距离当前时间N天前的日期
export const getDateNDaysAgo = function (n: number) {
  const currentDate = new Date() // 获取当前时间
  const targetDate = new Date(currentDate.getTime() + n * 24 * 60 * 60 * 1000) // 计算目标日期
  return getNowTime_yyyyMMddHHmmss(targetDate)
}

// 获取yyyy-MM-dd HH:mm:ss
export const getNowTime_yyyyMMddHHmmss = function (date: Date) {
  // console.log(date,"11111");
  const year = date.getFullYear()
  const month = (date.getMonth() + 1).toString().padStart(2, '0')
  const day = date.getDate().toString().padStart(2, '0')
  const hours = date.getHours().toString().padStart(2, '0')
  const minutes = date.getMinutes().toString().padStart(2, '0')
  const seconds = date.getSeconds().toString().padStart(2, '0')
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

// 获取yyyy-MM-dd HH:mm:ss
export const getyyyyMMddHHmmss_yyyyMMddHHmmss = function (date: number) {
  const year = date.toString().substring(0, 4)
  const month = date.toString().substring(4, 6)
  const day = date.toString().substring(6, 8)
  const hours = date.toString().substring(8, 10)
  const minutes = date.toString().substring(10, 12)
  const seconds = date.toString().substring(12, 14)
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

// 获取yyyyMMdd
export const getYyyyMMdd_date = function (date: Date) {
  const year = date.getFullYear()
  const month = (date.getMonth() + 1).toString().padStart(2, '0')
  const day = date.getDate().toString().padStart(2, '0')
  return `${year}${month}${day}`
}

//增加或减去小时数
export const getDateAddHours = function (date: Date, HH: number) {
  date.setHours(date.getHours() + HH)
  return date
}

//增加或减去天数
export const getDateAddDay = function (date: Date, days: number) {
  date.setDate(date.getDate() + days)
  return date
}

/*********************************************获取时间数组的最大值-最小值*********************************************/
// 获取时间数组的最大值
export const getMaxTime = function (dates: Date[]) {
  const timestamps: number[] = dates.map((date) => date.getTime())
  const maxTimestamp: number = Math.max(...timestamps)
  const maxDate: Date = new Date(maxTimestamp)
  return maxDate
}

// 获取时间数组的最大值
export const getMaxTimePlus = function (dates0: Date[], dates1: Date[]) {
  const dates: Date[] = dates0.concat(dates1)
  return getMaxTime(dates)
}

// 获取时间数组的最小值
export const getMixTime = function (dates: Date[]) {
  const timestamps: number[] = dates.map((date) => date.getTime())
  const minTimestamp: number = Math.min(...timestamps)
  const minDate: Date = new Date(minTimestamp)
  return minDate
}

// 获取时间数组的最小值
export const getMixTimePlus = function (dates0: Date[], dates1: Date[]) {
  const dates: Date[] = dates0.concat(dates1)
  return getMixTime(dates)
}
/*********************************************获取时间数组的最大值-最小值*********************************************/

export function getSysData(): string {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const day = String(now.getDate()).padStart(2, '0');
  const hours = String(now.getHours()).padStart(2, '0');
  const minutes = String(now.getMinutes()).padStart(2, '0');
  const seconds = String(now.getSeconds()).padStart(2, '0');

  return `${year}${month}${day}${hours}${minutes}${seconds}`;
}
